var GraceJS = require("../../Grace.JS/grace.js");
import {apiServer} from '../../common.js';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        typeList:[],
        type_id:'',
        page: 1,
        limit: 10,
        list: [],
        loadingType: 1,
        isEmpty: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getTypeList();
        this.getProjectList();
    },

    //下拉刷新事件
    onPullDownRefresh: function () {
        this.setData({
            list: [],
            page: 1,
            limit: 10,
            type:'',
            isEmpty: false,
        });
        this.getProjectList();
        // 结束下拉刷新
        wx.stopPullDownRefresh();
        GraceJS.msg('刷新成功');
    },

    // 点击分类
    setType: function(e){
        var that = this;
        that.setData({
            type_id: e.currentTarget.dataset.id,
            page: 1,
            limit: 10,
            list: []
        }, function(){
            that.getProjectList()
        })
    },

    // 获取作品
    getProjectList: function () {
        var that = this;
        wx.showLoading({
            title: 'Loading...',
        })
        GraceJS.get(
            apiServer + 'resume.project/index', {
                page: that.data.page,
                limit: that.data.limit,
                type_id: that.data.type_id,
            }, {},
            function (res) {
                if (res.code == 0) {
                    var nowList = GraceJS.arrayConcat(that.data.list, res.data);
                    that.setData({
                        list: nowList,
                        isEmpty: false,
                        page: that.data.page + 1,
                        loadingType: res.data.length < that.data.limit ? 3 : 1,
                    }, function () {
                        if (that.data.list.length == 0) {
                            that.setData({
                                isEmpty: true
                            })
                        }
                        wx.hideLoading();
                    })
                } else {
                    GraceJS.msg(res.message)
                }
            }
        )
    },

    // 获取分类
    getTypeList: function(){
        var that = this;
        GraceJS.get(
            apiServer + 'resume.project/projectType', 
            {}, {},
            function (res) {
                if (res.code == 0) {
                    that.setData({
                        typeList: res.data,
                    })
                } else {
                    GraceJS.msg(res.message)
                }
            }
        )
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        var that = this;
        //避免多次触发
        if (that.data.loadingType == 3) {
            return;
        }
        that.getProjectList();
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
            title: '郑炳东的作品',
            path: '/pages/work/index',
        }
    },

    // 分享朋友圈
    onShareTimeline: function () {
        return {
            title: '郑炳东的作品',
            query: '/pages/work/index',
        }
    }
})